package studiodiva.portfolio.client.lib.view;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;

public class LinkButton extends View<Hyperlink> implements MouseOverHandler, MouseOutHandler,
    MouseDownHandler, MouseUpHandler {
  private static final String ACTIVE = "active";
  private static final String DOWN = "down";
  private static final String OVER = "over";

  public LinkButton() {
    initWidget(new Hyperlink());
    addStyleName("button");
    addMouseOverHandler(this);
    addMouseOutHandler(this);
    addMouseDownHandler(this);
    addMouseUpHandler(this);
  }

  public void onMouseDown(MouseDownEvent event) {
    addStyleName(DOWN);
  }

  public void onMouseOut(MouseOutEvent event) {
    removeStyleName(OVER);
  }

  public void onMouseOver(MouseOverEvent event) {
    addStyleName(OVER);
  }

  @Override
  public void onMouseUp(MouseUpEvent event) {
    removeStyleName(DOWN);
  }

  public void setActive(boolean value) {
    setStyleName(ACTIVE, value);
  }

  public void setImage(ImageResource image) {
    DOM.appendChild(DOM.getFirstChild(asWidget().getElement()), new Image(image).getElement());
  }

  public void setTarget(String target) {
    widget().setTargetHistoryToken("!" + target);
  }

  public void setText(String value) {
    widget().setText(value.trim());
  }
}
